//
//  UIView+BaseBaseBusiness.m
//  LBBase
//
//  Created by Gary on 15/9/17

//  Copyright (c) 2015年 Lebo. All rights reserved.
//

#import "UIView+BaseBusiness.h"
#import "Common.h"
#import "UIImageView+UIActivityIndicatorForSDWebImage.h"
#import "UIButton+UIActivityIndicatorForSDWebImage.h"
#import <objc/runtime.h>

static const NSTimeInterval kAnimationDuration = 0.7f;

@implementation UIView (BaseBusiness)

- (void)becomeThinLine:(UIColor*)lineColor faceAtDirection:(ThinLineFaceAtDirection)direction {
    self.backgroundColor = [UIColor clearColor];
    CGSize size = self.bounds.size;
    CGRect frame;
    switch (direction) {
        case ThinLineFaceAtDirectionLeft:
            frame = MakeRect(0, 0, kSeperatorLineThickness, size.height);
            break;
            
        case ThinLineFaceAtDirectionRight:
            frame = MakeRect(size.width - kSeperatorLineThickness, 0, kSeperatorLineThickness, size.height);
            break;
            
        case ThinLineFaceAtDirectionTop:
            frame = MakeRect(0, 0, size.width, kSeperatorLineThickness);
            break;
            
        case ThinLineFaceAtDirectionBottom:
            frame = MakeRect(0, size.height - kSeperatorLineThickness, size.width, kSeperatorLineThickness);
            break;
            
        default:
            return;
            break;
    }
    
    UIView *line = [[UIView alloc] initWithFrame:frame];
    line.backgroundColor = lineColor;
    [self addSubview:line];
}

/**
 *  CATransition动画实现
 *
 *  @param type
 *  @param subtype 候选：kCATransitionFromLeft
 *                      kCATransitionFromBottom
 *                      kCATransitionFromRight
 *                      kCATransitionFromTop
 */
- (void)animate:(AnimationType)type subtype:(NSString *)subtype {
    switch (type) {
        case AnimationTypeFade:
        [self transitionWithType:kCATransitionFade WithSubtype:subtype];
        break;
        
        case AnimationTypePush:
        [self transitionWithType:kCATransitionPush WithSubtype:subtype];
        break;
        
        case AnimationTypeReveal:
        [self transitionWithType:kCATransitionReveal WithSubtype:subtype];
        break;
        
        case AnimationTypeMoveIn:
        [self transitionWithType:kCATransitionMoveIn WithSubtype:subtype];
        break;
        
        case AnimationTypeCube:
        [self transitionWithType:@"cube" WithSubtype:subtype];
        break;
        
        case AnimationTypeSuckEffect:
        [self transitionWithType:@"suckEffect" WithSubtype:subtype];
        break;
        
        case AnimationTypeOglFlip:
        [self transitionWithType:@"oglFlip" WithSubtype:subtype];
        break;
        
        case AnimationTypeRippleEffect:
        [self transitionWithType:@"rippleEffect" WithSubtype:subtype];
        break;
        
        case AnimationTypePageCurl:
        [self transitionWithType:@"pageCurl" WithSubtype:subtype];
        break;
        
        case AnimationTypePageUnCurl:
        [self transitionWithType:@"pageUnCurl" WithSubtype:subtype];
        break;
        
        case AnimationTypeCameraIrisHollowOpen:
        [self transitionWithType:@"cameraIrisHollowOpen" WithSubtype:subtype];
        break;
        
        case AnimationTypeCameraIrisHollowClose:
        [self transitionWithType:@"cameraIrisHollowClose" WithSubtype:subtype];
        break;
        
        case AnimationTypeCurlDown:
        [self animationTransition:UIViewAnimationTransitionCurlDown];
        break;
        
        case AnimationTypeCurlUp:
        [self animationTransition:UIViewAnimationTransitionCurlUp];
        break;
        
        case AnimationTypeFlipFromLeft:
        [self animationTransition:UIViewAnimationTransitionFlipFromLeft];
        break;
        
        case AnimationTypeFlipFromRight:
        [self animationTransition:UIViewAnimationTransitionFlipFromRight];
        break;
        
        default:
        break;
    }
}

- (void)transitionWithType:(NSString *)type WithSubtype:(NSString *)subtype {
    //创建CATransition对象
    CATransition *animation = [CATransition animation];
    //设置运动时间
    animation.duration = kAnimationDuration;
    //设置运动type
    animation.type = type;
    if (subtype != nil) {
        //设置子类
        animation.subtype = subtype;
    }
    //设置运动速度
    animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
    [self.layer addAnimation:animation forKey:@"animation"];
}

- (void)animationTransition:(UIViewAnimationTransition)transition {
    [UIView animateWithDuration:kAnimationDuration animations:^{
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationTransition:transition forView:self cache:YES];
    }];
}

@end

@implementation UIImageView (BaseBusiness)

- (void)loadImageUsingIndicator:(NSURL *)url placeholder:(UIImage *)placeholder {
    [self setImageWithURL:url
         placeholderImage:placeholder
                  options:SDWebImageDelayPlaceholder
usingActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
}

@end

@implementation UIButton (EnlargeEdge)
static char topNameKey;
static char rightNameKey;
static char bottomNameKey;
static char leftNameKey;

- (void)setEnlargeEdge:(CGFloat) size
{
    objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:size], OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void)setEnlargeEdgeWithTop:(CGFloat) top right:(CGFloat) right bottom:(CGFloat) bottom left:(CGFloat) left
{
    objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:top], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:right], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:bottom], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:left], OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (CGRect)enlargedRect
{
 NSNumber* topEdge    = objc_getAssociatedObject(self, &topNameKey);
 NSNumber* rightEdge  = objc_getAssociatedObject(self, &rightNameKey);
 NSNumber* bottomEdge = objc_getAssociatedObject(self, &bottomNameKey);
 NSNumber* leftEdge   = objc_getAssociatedObject(self, &leftNameKey);
    if (topEdge && rightEdge && bottomEdge && leftEdge)
    {
        return CGRectMake(self.bounds.origin.x - leftEdge.floatValue,
                          self.bounds.origin.y - topEdge.floatValue,
                          self.bounds.size.width + leftEdge.floatValue + rightEdge.floatValue,
                          self.bounds.size.height + topEdge.floatValue + bottomEdge.floatValue);
    }
    else
    {
        return self.bounds;
    }
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
 CGRect rect          = [self enlargedRect];
    if (CGRectEqualToRect(rect, self.bounds))
    {
        return [super pointInside:point withEvent:event];
    }
    return CGRectContainsPoint(rect, point) ? YES : NO;
}
/*
 - (UIView*)hitTest:(CGPoint) point withEvent:(UIEvent*) event
 {
 CGRect rect          = [self enlargedRect];
 if (CGRectEqualToRect(rect, self.bounds))
 {
 return [super hitTest:point withEvent:event];
 }
 return CGRectContainsPoint(rect, point) ? self : nil;
 }*/
@end